<template>
	<view class="login-container">
		<image class="logo" src="/static/logo.png"></image>
		<view class="form">
			<input class="input" v-model="account" placeholder="请输入账号" />
			<input class="input" v-model="password" placeholder="请输入密码" password />
		</view>
		<button class="login-btn" @click="submit">{{ isRegister ? '注册' : '登录' }}</button>
		<view class="switch-tip" @click="isRegister = !isRegister">
			{{ isRegister ? '已有账号？去登录' : '没有账号？去注册' }}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				account: '',
				password: '',
				isRegister: false
			}
		},
		methods: {
			// 清空上一个用户的健康数据缓存
			clearPreviousUserData() {
				// 清除健康数据相关缓存
				uni.removeStorageSync('healthData')
				uni.removeStorageSync('lastAnalysis')
				uni.removeStorageSync('adviceFavorites')
				uni.removeStorageSync('customReminders')

				console.log('已清空上一个用户的健康数据缓存')

				// 如果当前在健康数据页面，强制刷新页面
				const pages = getCurrentPages()
				const currentPage = pages[pages.length - 1]
				if (currentPage && currentPage.route && currentPage.route.includes('health')) {
					// 强制刷新健康数据页面
					currentPage.loadHealthData()
					console.log('已刷新健康数据页面')
				}
			},
			submit() {
				if (!this.account || !this.password) {
					uni.showToast({
						title: '请输入账号和密码',
						icon: 'none'
					})
					return
				}
				const url = this.isRegister ?
					'http://127.0.0.1:3000/api/register' :
					'http://127.0.0.1:3000/api/login'
				uni.request({
					url,
					method: 'POST',
					data: {
						account: this.account,
						password: this.password
					},
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if (res.data && res.data.token) {
							// 登录成功，清空上一个用户的健康数据缓存
							this.clearPreviousUserData()

							uni.setStorageSync('userInfo', res.data.userInfo)
							uni.setStorageSync('token', res.data.token)
							uni.showToast({
								title: this.isRegister ? '注册成功' : '登录成功',
								icon: 'success'
							})
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/user/user'
								})
							}, 1000)
						} else {
							uni.showToast({
								title: res.data.error || '操作失败',
								icon: 'none'
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped>
	.login-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100vh;
	}

	.logo {
		width: 180rpx;
		height: 180rpx;
		margin-bottom: 60rpx;
	}

	.form {
		width: 80vw;
		margin-bottom: 30rpx;
	}

	.input {
		width: 100%;
		height: 90rpx;
		border: 2rpx solid #e0e0e0;
		border-radius: 10rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		background: #fafafa;
		margin-bottom: 20rpx;
	}

	.login-btn {
		width: 80vw;
		height: 90rpx;
		background: linear-gradient(135deg, #4CAF50, #45a049);
		color: white;
		border: none;
		border-radius: 15rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.switch-tip {
		color: #4CAF50;
		margin-top: 30rpx;
		font-size: 28rpx;
		text-decoration: underline;
	}
</style>